package org.hgvc.webapp.ets.jsf.beans.mastersetup;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;

import org.hgvc.webapp.ets.bo.Remark;
import org.hgvc.webapp.ets.constant.AdminConstant;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.spring.service.MasterSetupService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;


public class RemarkBean extends AbstractBean{
	public static final String BEAN_NAME="remarkBean";
	
	private MasterSetupService masterSetupService;
	
	private List<Remark> remarkList;
	private Remark remark;
	private boolean catchError;
	
	public Remark getRemark() {
		return remark;
	}
	public void setRemark(Remark remark) {
		this.remark = remark;
	}
	public List<Remark> getRemarkList() {
		return remarkList;
	}
	public void setMasterSetupService(MasterSetupService masterSetupService) {
		this.masterSetupService = masterSetupService;
	}
	
	public String initRemarkAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		try{
			this.remarkList=this.masterSetupService.getRemarkList();
			this.remark=new Remark();
		}catch(BaseException be){
			initErrorBean(be);
		}
		return outcome;
	}
	
	public String navAddRemarkAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void navAddRemarkActionEvent(ActionEvent event){
		this.catchError=false;
		this.remark=new Remark();
	}
	public String addRemarkAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void addRemarkActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.remark.setCreatedBy(getThisUser());
			this.remark.setCreatedDate(new Date());
			this.remark.setIsDeleted(CommonConstant.NOT_DELETED);
			this.masterSetupService.saveRemark(this.remark);
			this.remarkList=this.masterSetupService.getRemarkList();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String navUpdateRemarkAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void navUpdateRemarkActionEvent(ActionEvent event){
		this.catchError=false;
		UIParameter param=(UIParameter)event.getComponent().findComponent("remarkId");
		String remarkId=param.getValue().toString();
		for(Remark remarkP : this.remarkList){
			if (remarkP.getRemarkId().equalsIgnoreCase(remarkId)){
				this.remark=remarkP;
				break;
			}
		}
	}
	public String updateRemarkAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void updateRemarkActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.remark.setModifiedBy(getThisUser());
			this.remark.setModifiedDate(new Date());
			this.masterSetupService.updateRemark(this.remark);
			this.remarkList=this.masterSetupService.getRemarkList();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String deleteRemarkAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void deleteRemarkActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			List<Remark> deleteList=new ArrayList<Remark>();
			for(Remark remarkP : this.remarkList){
				if (remarkP.isMarkAsSelect())
					deleteList.add(remarkP);
			}
			this.masterSetupService.deleteRemark(deleteList);
			this.remarkList=this.masterSetupService.getRemarkList();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
}
